<?php

namespace teach\service;

use Yii;
class Order
{
    public function orderList($condition, $page, $pre_page = 10)
    {
        $count = $this->orderCount($condition);
        $data = ['total' => 0, 'total_page' => 0, 'page' => 1, 'pre_page' => $pre_page, 'lists' => []];;

        if ($count) {
            $total_page = ceil($count / $pre_page);

            $sql = "SELECT * FROM edu_active_order WHERE " . $condition . " limit " . ($page - 1) * $pre_page . "," . $pre_page;
            $res = Yii::$app->db->createCommand($sql)->queryAll();

            $data = ['total' => $count, 'total_page' => $total_page, 'page' => $page, 'pre_page' => $pre_page, 'lists' => $res];
        }
        return $data;
    }

    /**
     * 查询符合条件的数量
     * @param $condition
     * @return false|null|string
     */
    public function orderCount($condition)
    {
        $sql = "SELECT COUNT(*) FROM edu_active_order WHERE  {$condition} ";
        return Yii::$app->db->createCommand($sql)->queryScalar();
    }
}